package org.helgoboss.document_database_orient_impl

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx
import com.orientechnologies.orient.core.record.impl.ODocument
import com.orientechnologies.orient.core.metadata.schema.OClass
import org.helgoboss.document_database._

class OrientBucket(val database: OrientDatabase, private[document_database_orient_impl] val delegate: OClass) extends Bucket {
    val id = delegate.getName

    def documents = {
        import scala.collection.JavaConversions.iterableAsScalaIterable
        database.delegate.browseClass(delegate.getName).map { oDocument =>
            new OrientDocument(bucket = this, delegate = oDocument)
        }
    }
        
    def removeDocument(document: Document) {
        database.delegate.delete(document.asInstanceOf[OrientDocument].delegate)
    }
    
    def addDocument = {
        val oDocument = new ODocument(database.delegate, delegate.getName)
        new OrientDocument(bucket = this, delegate = oDocument)
    }
    
    def clear {
        documents.foreach(removeDocument(_))
    }
}